﻿using Max.PH5_2006.ShoppingSite.Domain;
using Max.PH5_2006.ShoppingSite.DTO;
using Max.PH5_2006.ShoppingSite.IService;
using Max.PH5_2006.ShoppingSite.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH5_2006.ShoppingSite.API.Controllers
{
    [Route("member")]
    [ApiController]
    public class MemberController : ControllerBase
    {
        IMemberService _service;
        IMemberAddressService _addressService;
        public MemberController(IMemberService service, IMemberAddressService addressService)
        {
            _service = service;
            _addressService = addressService;
        }

        //[HttpPost]
        //public IActionResult Login(MemberEntity model)
        //{
        //    ResponseResultDto response = new();
        //    try
        //    {
        //        var result = _service.Login(model);

        //        if (result.Item1)
        //            response.Data = result.Item3;
        //        else
        //        {
        //            response.Message = result.Item2;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        response.Code = Domain.Shared.ResponseResultStatue.请求失败;
        //        response.Message = ex.Message;
        //    }

        //    return Ok(response);
        //}



        #region 会员地址相关

        //添加会员地址
        [HttpPost("address/add")]
        public IActionResult PostAddress(MemberAddressEntity entity)
        {
            ResponseResultDto response = new();

            _addressService.AddAddress(entity);

            return Ok(response);
        }

        //显示会员地址
        [HttpGet("address/query")]
        public IActionResult GetAddress(string memberId)
        {
            ResponseResultDto response = new();

            //var data = _addressService.Query()
            //                .Where(x => memberId.Equals(x.MemberId.ToString()))    //只查这个会员的收货地址
            //                .OrderByDescending(x=>x.IsDefault);     //把默认地址放最上面

            var data = _addressService.Query(memberId);

            response.Data = data;

            return Ok(response);
        }

        //删除会员地址
        [HttpDelete("address/delete")]
        public IActionResult DeleteAddress(string id)
        {
            ResponseResultDto response = new();

            List<dynamic> ids = new();
            ids.Add(new Guid(id));
            _addressService.Delete(ids);

            return Ok(response);
        }



        #endregion
    }
}
